package com.kun.index.service.impl;

import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kun.common.Constants;
import com.kun.index.entity.Attach;
import com.kun.index.mapper.AttachMapper;
import com.kun.index.service.AttachService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kun.utils.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;


/**
 * <p>
 * 附件表 服务实现类
 * </p>
 *
 * @author kunkun
 * @since 2024-02-29
 */
@Service
public class AttachServiceImpl  extends ServiceImpl<AttachMapper, Attach> implements AttachService {

    @Autowired
    private AttachMapper attachMapper;

    @Value("${file.upload.blogPicturePath}")
    private String uploadBlogPicturePath;

    @Override
    public String uploadAttach(MultipartFile file) {

//        1. 获得原始名字
        String originalFilename = file.getOriginalFilename();
//        2. 获取文件类型
        String type = FileNameUtil.extName(originalFilename);
//        3. 获取文件大小,单位kb
        long size = file.getSize() / 1024;
//        4. 磁盘位置判断，判断该目录是否存在，不存在则重新创建
        File uploadParentPath = new File(uploadBlogPicturePath);
        if (!uploadParentPath.exists()){
            uploadParentPath.mkdirs();
        }
//        5. 生成md5
        File file1 = null;
        try {
            file1 = FileUtils.toFile(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String md5 = SecureUtil.md5(file1);
        File file2 = new File(file1.toURI());
        if (file2.delete()){
            System.out.println("删除成功");
        }else {
            System.out.println("删除失败");
        }

        String url="";

//        6. 判断md5是否存在，若存在，则不进行文件存储；反之，存储文件
        QueryWrapper<Attach> wrapper = new QueryWrapper<>();
        wrapper.eq("fileMd5",md5);
        List<Attach> attaches = attachMapper.selectList(wrapper);
        if (attaches.size()>0){
            url=attaches.get(0).getUrl();
        }else{
            String currentFileName = IdUtil.fastSimpleUUID();
            File currentFile = new File(uploadBlogPicturePath + currentFileName +"."+ type);
            try {
                file.transferTo(currentFile);
            } catch (IOException e) {
                e.printStackTrace();
            }
            url= currentFileName+"."+ type;
        }

        Attach newAttach = new Attach();
        newAttach.setName(originalFilename);
        newAttach.setSize(size);
        newAttach.setType(type);
        newAttach.setUrl(url);
        newAttach.setFileMd5(md5);
        attachMapper.insert(newAttach);
        return url;


    }

    @Override
    public String uploadAttachByString(String fileLocalAddress) {
        File file = new File(fileLocalAddress);
        String name= this.uploadAttach(FileUtils.toMultipartFile(file));

        return name;
    }
}
